package ccnt.pvc.scudwaremobile.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import ccnt.pvc.scudwaremobile.R;
import ccnt.pvc.scudwaremobile.bean.ResultObject;
import ccnt.pvc.scudwaremobile.utils.Constants;
import ccnt.pvc.scudwaremobile.utils.Constants.PreferenceNames;
import ccnt.pvc.scudwaremobile.utils.HttpClientUtils;
import ccnt.pvc.scudwaremobile.utils.ThreadPoolUtils;

public class LoginActivity extends Activity implements OnClickListener {

	private static final int LOGIN_SUCCESS = 1;
	private static final int LOGIN_ERROR = 2;
	private static final int SERVER_NO_RESPONSE = 3;
	
	Button btnLogin;
	EditText editAccount;
	EditText editPwd;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		
		initView();
	}

	private void initView() {
		btnLogin = (Button)findViewById(R.id.btnLogin);
		btnLogin.setOnClickListener(this);
		editAccount = (EditText)findViewById(R.id.editAccount);
		editPwd = (EditText)findViewById(R.id.editPwd);
	}
	
	@Override
	public void onClick(View view) {
		int id = view.getId();
		switch(id) {
		case R.id.btnLogin:
			login();
			break;
		}
	}
	
	private void login() {
		final String account = editAccount.getText().toString();
		final String password = editPwd.getText().toString();
		if (account.equals("") || password.equals("")) {
			Toast.makeText(LoginActivity.this, R.string.login_warn_null,
					Toast.LENGTH_SHORT).show();
		} else {
			Toast.makeText(LoginActivity.this, R.string.login_info_logining,
					Toast.LENGTH_SHORT).show();
			
			//在另外一个线程中执行登录操作
			ThreadPoolUtils.execute(new Runnable() {
				
				@Override
				public void run() {
					
					if(account.equals("test") && password.equals("test")) {
						Message msg = Message.obtain(null, LOGIN_SUCCESS, 0, 0);
						handler.sendMessage(msg);
						return;
					}
					
					List<NameValuePair> params = new ArrayList<NameValuePair>();
					params.add(new BasicNameValuePair("userName", account));
					params.add(new BasicNameValuePair("userPwd", password));
					String url = Constants.SERVER_URL + "/user/login_remote.do";
					
					try {
						Thread.sleep(2000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					
					ResultObject resultObject = HttpClientUtils.sendPost(url, params);
					
					// e.g.
					// {"result":-1,"description":"integer: indicate the login user ID or not","code":200}
					int code = resultObject.getCode();
					if(code == 200) {
						if(resultObject.getResult().equals("-1")) {
							Message msg = Message.obtain(null, LOGIN_ERROR, 0, 0);
							handler.sendMessage(msg);
						} else {
							Message msg = Message.obtain(null, LOGIN_SUCCESS, 0, 0);
							handler.sendMessage(msg);
						}
					} else {
						Message msg = Message.obtain(null, SERVER_NO_RESPONSE, 0, 0);
						handler.sendMessage(msg);
					}
				}
			});
			
		}
	}
	
	private void onSuccessLogin() {
		Toast.makeText(LoginActivity.this, R.string.login_success,
				Toast.LENGTH_SHORT).show();
		// 保存用户名和已登录记录
		SharedPreferences preference = getSharedPreferences(
				PreferenceNames.LOGIN_PREFERENCE, MODE_PRIVATE);
		Editor editor = preference.edit();
		editor.putString("account", editAccount.getText().toString());
		editor.putBoolean("isLogin", true);
		editor.commit();
		
		startActivity(new Intent(LoginActivity.this, MainActivity.class));
		this.finish();
	}
	
	private Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			switch(msg.what) {
			case LOGIN_SUCCESS:
				onSuccessLogin();
				break;
			case LOGIN_ERROR:
				Toast.makeText(LoginActivity.this, R.string.login_warn_error,
						Toast.LENGTH_SHORT).show();
				break;
			case SERVER_NO_RESPONSE:
				Toast.makeText(LoginActivity.this, R.string.login_warn_no_response,
						Toast.LENGTH_SHORT).show();
				break;
			default:
					super.handleMessage(msg);
			}
			
		}
		
	};
}
